package com.zx.feign.client.fallbackfactory;

import com.zx.core.wrapper.ListWrapper;
import com.zx.core.wrapper.ObjectWrapper;
import com.zx.feign.client.DictServiceClient;
import com.zx.feign.client.dto.DictDataDTO;
import com.zx.feign.client.dto.DictTypeDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * description: 字典服务客户端降级工厂<br>
 * create: 2025-11-19 <br>
 * </p>
 *
 * @author zhou xun
 */
@Slf4j
@Component
public class DictServiceClientFallbackFactory implements FallbackFactory<DictServiceClient> {

    @Override
    public DictServiceClient create(Throwable cause) {
        return new DictServiceClient() {
            @Override
            public ListWrapper<DictDataDTO> getDictDataByType(String dictType) {
                log.error("字典服务调用失败，触发降级处理", cause);
                ListWrapper<DictDataDTO> result = new ListWrapper<>();
                result.fail("字典服务暂时不可用");
                return result;
            }

            @Override
            public ObjectWrapper<Map<String, List<DictDataDTO>>> getBatchDictData(List<String> dictTypes) {
                log.error("字典服务调用失败，触发降级处理", cause);
                ObjectWrapper<Map<String, List<DictDataDTO>>> result = new ObjectWrapper<>();
                result.fail("字典服务暂时不可用");
                return result;
            }

            @Override
            public ListWrapper<DictTypeDTO> getDictTypes() {
                log.error("字典服务调用失败，触发降级处理", cause);
                ListWrapper<DictTypeDTO> result = new ListWrapper<>();
                result.fail("字典服务暂时不可用");
                return result;
            }
        };
    }
}